<?php 
    
	require_once '../funciones.php';

        $soporte=$_SESSION['caweb']['soporte'];
	$page = $_REQUEST['page']; 

	// get how many rows we want to have into the grid - rowNum parameter in the grid 
	$limit = $_REQUEST['rows']; 

	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel 
	$sidx = $_REQUEST['sidx']; 

	// sorting order - at first time sortorder 
	$sord = $_REQUEST['sord']; 

	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1;

        //Validacion Datos de la consulta
        $fechaIni   = ($_REQUEST['fechaini'] != "")?$_REQUEST['fechaini']:'1986-01-01';
        $fechaFin   = ($_REQUEST['fechaFin'] != "")?$_REQUEST['fechaFin']:'2999-01-01';
        $nit        = $_REQUEST['nit'];
        $numAbono    = $_REQUEST['numAbono'];
        $sucursal   = $_REQUEST['sucursal'];
        //Se construye la condicion
        $where = ($nit != "")?" and cliente = '$nit'":"";
        $where .= ($factura != "")?" and numeroAbono = '$numAbono'":"";
        $where .= ($sucursal != "")?" and sucursal = '$sucursal'":"";
        $con = con_caweb();
	// calculate the number of rows for the query. We need this for paging the result 
	$result = mysql_query("SELECT COUNT(*) AS count FROM cabeceraabonosId where estado not in ('A','V') and fecharecaudo between '$fechaIni' and '$fechaFin' $where ");
	$row = mysql_fetch_array($result,MYSQL_ASSOC); 
	$count = $row['count']; 

	// calculate the total pages for the query 
	if( $count > 0 ) $total_pages = ceil($count/$limit); 
	else $total_pages = 0;

	// if for some reasons the requested page is greater than the total 
	// set the requested page to total page 
	if ($page > $total_pages) $page=$total_pages;

	// calculate the starting position of the rows 
	$start = $limit*$page - $limit;

	// if for some reasons start position is negative set it to 0 
	// typical case is that the user type 0 for the requested page 
	if($start <0) $start = 0; 

	// the actual query for the grid data
//	$SQL = "SELECT * FROM abonosfacturacion WHERE transaccion > 0 GROUP BY transaccion ORDER BY $sidx $sord LIMIT $start , $limit";

	$SQL = "
	SELECT cab.estado,cab.idabonos,cab.numeroAbono,cab.fecharecaudo,cab.sucursal,cab.valrecaudo,cab.cliente,man.name,cab.tipo,cab.clase,cab.banco,cab.cheque,cab.cuenta,cab.usuariocreo from cabeceraabonosId cab
	LEFT JOIN  clientes AS man ON man.nit = cab.cliente
        where cab.estado not in ('A','V') and fecharecaudo between '$fechaIni' and '$fechaFin' $where ORDER BY $sidx $sord LIMIT $start , $limit";

	$result = mysql_query( $SQL ) or die("Error de consulta.".mysql_error().$SQL);

	// we should set the appropriate header information
	if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
				  header("Content-type: application/xhtml+xml;charset=utf-8"); 
	} else {
			  header("Content-type: text/xml;charset=utf-8");
	}
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<rows>";
	echo "<page>".$page."</page>";
	echo "<total>".$total_pages."</total>";
	echo "<records>".$count."</records>";

	$SQL2 = "
	SELECT sum(cab.valrecaudo) as suma from cabeceraabonosId cab
	LEFT JOIN  clientes AS man ON man.nit = cab.cliente
        where cab.estado not in ('A','V') and fecharecaudo between '$fechaIni' and '$fechaFin' $where ";
        $result1 = mysql_query($SQL2) or die("Error SQL 3".mysql_error().$SQL2);
        echo "<row id='a'>";
        echo "<cell></cell><cell></cell><cell></cell>";
        echo "<cell>TOTAL</cell>";
        echo "<cell>". number_format(mysql_result($result1, 0,"suma"),0, ',', '.')."</cell>";
        echo "<cell></cell><cell></cell><cell></cell><cell></cell><cell></cell><cell></cell><cell></cell><cell></cell>";
        echo "</row>";


	// be sure to put text data in CDATA
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		echo "<row id='". $row[idabonos]."'>";
		echo "<cell>". $row[numeroAbono]."</cell>";
                $estado = ($row[estado] == "P")?"Pendiente Aplicar":"Generado";
                echo "<cell>". $estado."</cell>";
		echo "<cell>". $row[fecharecaudo]."</cell>";
		echo "<cell>". $row[sucursal]."</cell>";
		echo "<cell>". number_format($row[valrecaudo],0, ',', '.')."</cell>";
		echo "<cell>". $row[cliente]."</cell>";
		echo "<cell>". utf8_encode(htmlspecialchars($row[name]))."</cell>";
		echo "<cell>". $row[tipo]."</cell>";
		echo "<cell>". $row[clase]."</cell>";
		echo "<cell>". $row[cheque]."</cell>";
		echo "<cell>". $row[banco]."</cell>";
                $sql = "select cuentarecaudo from cuentasrecaudos where id = ".$row['cuenta'];
                $sql = mysql_query($sql,$con) or die("Error SQL 8".mysql_error().$sql);
		echo "<cell>". mysql_result($sql, 0,"cuentarecaudo")."</cell>";
		echo "<cell>". $row[usuariocreo]."</cell>";
		echo "</row>";
	}
	echo "</rows>";
	 
?>
